var app = getApp();
Component({
    properties: {
        dateday : Object,
        params : Object,
        isprice : Number,
        pricearr : Array,
        priprice : Number, // 需要支付的价格
        datearrtime : Array,
        gid : Number,
        carttid : Number,
        mapid : String,
    },
    data: {
        year: 0,
        month: 0,
        date: ['日', '一', '二', '三', '四', '五', '六'],
        dateArr: [],
        isToday: 0,
        isTodayWeek: false,
        todayIndex: 0,
        actday : '',
        isshow : false,
        acttime : '',
        actdate : '',
        downstockarr : [],
    },
    options : {
        addGlobalClass : true,
    },
    methods: {
        gettime(e){
            var index = e.currentTarget.dataset.i;
            if( this.data.downstockarr[index].stock*1 == 0 ){
                return false;
            }
            this.setData({
                acttime : this.data.downstockarr[index].name,
            });
        },
        showthis(){
            this.dateInit();
        },
        hidethis(){
            this.setData({
                isshow : false,
            })
        },
        dateInit: function (setYear, setMonth) {
            var self = this;

            if( !setYear ){

                let nowa = new Date();
                let yeara = nowa.getFullYear();
                let montha = nowa.getMonth() + 1;
                
                var starty =  this.data.dateday.starty ? this.data.dateday.starty*1 : yeara;
                var startm =  this.data.dateday.startm ? this.data.dateday.startm*1 : montha;
                var startd =  this.data.dateday.startd ? this.setnum(this.data.dateday.startd*1) : this.setnum(nowa.getDate());

                this.setData({
                    actday : '',
                    actdate : '',
                    acttime : '',
                    year: starty,
                    month: startm,
                    isToday: '' + starty + startm + startd,
                })
            }

            this.setData({
                downstockarr : [],
                actday : '',
                actdate : '',
                acttime : '',
            })

            //全部时间的月份都是按0~11基准，显示月份才+1
            let dateArr = [];                       //需要遍历的日历数组数据
            let arrLen = 0;                         //dateArr的数组长度
            let now = setYear ? new Date(setYear, setMonth) : new Date();
            let year = setYear || starty;
            let nextYear = 0;
            let month = setMonth || startm-1;                 //没有+1方便后面计算当月总天数
            let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
            let startWeek = new Date(year + '/' + (month + 1) + '/' + 1).getDay();                          //目标月1号对应的星期

            let dayNums = new Date(year, nextMonth, 0).getDate();               //获取目标月有多少天
            let obj = {};
            let num = 0;
            if (month + 1 > 11) {
              nextYear = year + 1;
              dayNums = new Date(nextYear, nextMonth, 0).getDate();
            }
            arrLen = startWeek*1 + dayNums*1;
            let stockarr = [];
            for (let i = 0; i < arrLen; i++) {
                if (i >= startWeek) {
                    num = i - startWeek + 1;
                    obj = {
                        isToday: '' + year + this.setnum((month + 1)) + this.setnum(num),
                        date : year +'-'+ this.setnum((month + 1)) +'-'+ this.setnum(num),
                        dateNum: num,
                        weight: 5,
                        status : 0,
                    }
                    if( obj.isToday*1 < this.data.dateday.start*1 || obj.isToday*1 > this.data.dateday.end*1 ){
                        obj.status = 1;
                    }else{
                        stockarr.push(obj.date);
                    }

                    if( this.data.isprice*1 == 1 ){
                        obj.price = this.data.priprice;

                        for (var j = 0; j < this.data.pricearr.length; j++) { 
                            if( this.data.pricearr[j].date == obj.date ){
                                let item = this.data.pricearr[j];
                                obj.price = this.data.paytype == 0 || item.showPricejb*1 <= 0 ? item.showPrice : item.showPricejb*1;
                                break;
                            }
                        }
                    }

                } else {
                    obj = {};
                }
                dateArr[i] = obj;
            }

            if(stockarr.length > 0){
                var postdata = {
                    op : 'checkstock',
                    gid : this.data.gid,
                    date : JSON.stringify(stockarr),
                    carttid : this.data.carttid ? this.data.carttid : '',
                    mapid : this.data.mapid ? this.data.mapid : '',
                }
                app.com.http('order','POST',postdata,0,true,'','',function(res){
                    if (!res.data.errno && res.data) {
                        for (var i = 0; i < dateArr.length; i++) {
                            for (var j = 0; j < res.data.data.length; j++) {
                                if( res.data.data[j].date == dateArr[i].date ){

                                    dateArr[i].stock = res.data.data[j].stock;
                                    dateArr[i].down = res.data.data[j].down;
                                    break;
                                }
                            }
                        }

                        self.setData({
                            dateArr: dateArr,
                            isshow : true,
                        })
                        self.settoday(setYear,setMonth);
                    }else{
                        app.com.alert(res.data.message);
                    }
                })
            }else{
                this.setData({
                    dateArr: dateArr,
                    isshow : true,
                })
                this.settoday(setYear,setMonth);
            }

        },
        settoday(setYear,setMonth){
            let nowDate = new Date();
            let nowYear = nowDate.getFullYear();
            let nowMonth = nowDate.getMonth() + 1;
            let nowWeek = nowDate.getDay();
            let getYear = setYear || nowYear;
            let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;
            if (nowYear == getYear && nowMonth == getMonth) {
                this.setData({
                    isTodayWeek: true,
                    todayIndex: nowWeek
                })
            } else {
                this.setData({
                    isTodayWeek: false,
                    todayIndex: -1
                })
            }
        },
        setnum(num){
            if( num*1 <= 9 ) return '0'+num;
            return num;
        },
        lastMonth: function () {
            //全部时间的月份都是按0~11基准，显示月份才+1
            let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
            let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
            this.setData({
              year: year,
              month: (month + 1)
            })
            this.dateInit(year, month);
        },
        nextMonth: function () {
            //全部时间的月份都是按0~11基准，显示月份才+1
            let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
            let month = this.data.month > 11 ? 0 : this.data.month;
            this.setData({
              year: year,
              month: (month + 1)
            })
            this.dateInit(year, month);
        },
        lookHuoDong(e){
            var index = e.currentTarget.dataset.i;
            if(this.data.dateArr[index].status*1 == 1 || this.data.dateArr[index].stock*1 == 0) return false;

            let day = this.data.dateArr[index].isToday;
            
            this.setData({
                actday: day,
                acttime : '',
                actdate : this.data.dateArr[index].date,
                downstockarr : this.data.dateArr[index].down ? this.data.dateArr[index].down : []
            })
        },
        comdatefn(){
            var self = this;
            if( !this.data.actdate ) {
                app.com.toast('请选择日期');
                return false;
            }
            if( this.data.downstockarr && this.data.downstockarr.length > 0 && !this.data.acttime ) {
                app.com.toast('请选择时间');
                return false;
            }
            this.triggerEvent('confirmdate',{date:this.data.actdate,time:this.data.acttime});
            this.hidethis();
        },

    }
});